package com.dtcnet.skmvp.utils;

import android.text.TextUtils;

import com.tencent.mmkv.MMKV;

public class MMKVUtils {

    private MMKV kv;

    private static MMKVUtils instance;

    private MMKVUtils() {
        kv = MMKV.defaultMMKV();
    }

    public static MMKVUtils getInstance() {
        if (instance == null) {
            synchronized (MMKVUtils.class) {
                if (instance == null) {
                    instance = new MMKVUtils();
                    return instance;
                }
                return instance;
            }
        }
        return instance;
    }

    public boolean saveString(String key, String value) {
        return kv.encode(key, value);
    }

    public String getString(String key, String def) {
        String val = kv.decodeString(key);
        if (TextUtils.isEmpty(val)) {
            val = def;
        }
        return val;
    }

    public boolean saveInt(String key, int value) {
        return kv.encode(key, value);
    }

    public int getInt(String key) {
        return kv.decodeInt(key);
    }

    public boolean saveLong(String key, long value) {
        return kv.encode(key, value);
    }

    public long getLong(String key) {
        return kv.decodeLong(key);
    }

    public boolean saveBool(String key, boolean value) {
        return kv.encode(key, value);
    }

    public boolean getBool(String key) {
        return kv.decodeBool(key);
    }
}
